/*
插件版本
201.11.16
*/

//调用实例
// var id = elayer.loading();

// elayer.alert('测试', '测试内容1', 
//     function () {
//         alert('确定回调');
//     }, function () {
//         alert('关闭回调');
//     }
// );

// elayer.confirm('测试测试', '你是否要关闭本页面?',
//     function () {
//         alert('yes回调');
//     },
//     false,
//     function () {
//         alert('关闭回调');
//     }
// );

// elayer.page('测试', $('<a>测试测试。。。。</a>'), 
//     function () {
//         alert('关闭回调');
//     }
// );

// elayer.close(id);
//弹出层插件
var elayer = (function () {
    var conf = {
        id: 0,
        zindex: 999,
        init: false
    };
    var edoctor = {
        init: function () {
            var id = conf.id = conf.id + 1;
            this.mask();
            return id;
        },
        getjspath: function (jsname) {
            var es = document.getElementsByTagName("script");
            for (var i = 0; i < es.length; i++) {
                if (es[i].src.indexOf(jsname) > -1) {
                    return es[i].src.substring(0, es[i].src.lastIndexOf("/") + 1);
                }
            }
            return null;
        },
        center: function (taget) {
            taget.css({
                top: '50%',
                left: '50%',
                'marginTop': taget.height() * -0.5,
                'marginLeft': taget.width() * -0.5
            });
        },
        mask: function () {
            conf.zindex = conf.zindex + 2;
            $('body').prepend('<div id="mask-' + conf.id + '" class="elayer-mask" style="z-index:' + conf.zindex + ';"></div>');
        },
        close: function (i) {
            if (i == undefined) this.closeall();
            else {
                $('#elayer-' + i + ',' + '#mask-' + i).fadeOut(350, function () {
                    $('#elayer-' + i).remove();
                    $('#mask-' + i).remove();
                });
            }
        },
        closeall: function () {
            $('.elayer-mask').remove();
            $('.elayer-view').remove();
        },
        loading: function () {
            this.init();
            var load = $('' +
                '<div id="elayer-' + conf.id + '" class="elayer-view" style="z-index:' + (conf.zindex + 1) + ';">' +
                '<div class="elayer-loading"></div>' +
                '</div>');
            $('body').prepend(load);
            this.center(load);
            return conf.id;
        },
        alert: function (t, c, okcall, closecall) {
            this.init();
            conf.init = true;
            var alert = $('<div><div class="elayer-alertcontent"></div></div><div class="elayer-sure"><a class="elayer-ok">确定</a></div>');
            alert.find('.elayer-alertcontent').append(c);
            var taget = this;
            alert.find('.elayer-ok').click(function () {
                if (okcall) okcall();
                else taget.close(conf.id);
            });
            return this.page(t, alert, closecall, true);
        },
        confirm: function (t, c, yescall, nocall, closecall) {
            this.init();
            conf.init = true;
            var confirm = $('<div>' + c +
                '</div><div class="elayer-confirm"><a class="elayer-no active">取消</a><a class="elayer-yes">确定</a></div>');
            var taget = this;
            var cancleid = conf.id;
            confirm.find('.elayer-yes').click(function () {
                yescall();
            });
            confirm.find('.elayer-no').click(function () {
                if (nocall) nocall();
                else taget.close(cancleid);
            });
            return this.page(t, confirm, closecall, true);
        },

        page: function (t, c, closecall, padding) {
            if (!conf.init) {
                this.init()
            };
            var page = $('' +
                '<div id="elayer-' + conf.id + '" class="elayer-view" data-id="' + conf.id + '" style="z-index:' + (conf.zindex + 1) + ';">' +
                '<div class="elayer-title"><h2>' + t + '<a class="elayer-close">×</a></h2></div>' +
                '<div class="elayer-content"></div>' +
                '</div>'
            );
            var taget = this;
            page.find('.elayer-content').append(c);
            page.find('.elayer-close').click(function () {
                taget.close(page.data('id'));
                if (closecall) closeall();
            });
            if (!padding) {
                page.find('.elayer-content').css('padding', '20px 0 0 0 ');
            }
            $('body').prepend(page);
            this.center(page);
            conf.init = false;
            return conf.id;
        },
        noborderpage: function (c, closecall, padding) {
            if (!conf.init) {
                this.init()
            };
            var page = $('' +
                '<div id="elayer-' + conf.id + '" class="elayer-view elayer-noborder" data-id="' + conf.id + '" style="z-index:' + (conf.zindex + 1) + ';">' +
                '<div class="elayer-content"><div><a class="elayer-close">×</a></div></div>' +
                '</div>'
            );
            var taget = this;
            page.find('.elayer-content').append(c);
            page.find('.elayer-close').click(function () {
                taget.close(page.data('id'));
                if (closecall) closeall();
            });
            if (!padding) {
                page.find('.elayer-content').css('padding', '0 ');
            }

            $('.elayer-mask').bind('click', function () {
                taget.closeall();
                $('.elayer-mask').unbind('click');
            });

            $('body').prepend(page);
            this.center(page);
            conf.init = false;
            return conf.id;
        },
        mobilepage: function (c, closecall, padding) {
            if (!conf.init) {
                this.init()
            };
            var page = $('' +
                '<div id="elayer-' + conf.id + '" class="elayer-view elayer-mobilepage" data-id="' + conf.id + '" style="z-index:' + (conf.zindex + 1) + ';">' +
                '<div class="elayer-content"><div><div><a class="elayer-close">×</a></div><div><a class="elayer-prevpage">返回</a></div></div></div>' +
                '</div>'
            );
            //<div><a class="elayer-nextpage">前进</a></div>
            var taget = this;
            page.find('.elayer-content').append(c);
            page.find('.elayer-close').click(function () {
                taget.close(page.data('id'));
                if (closecall) closeall();
            });
            page.find('.elayer-prevpage').click(function () {
                //上一页
                //console.log(window.frames['win'].history.length);
                window.history.back();
                //document.getElementById('win').contentWindow.history.go(-1);
            });
            page.find('.elayer-nextpage').click(function () {
                //下一页

            });
            if (!padding) {
                page.find('.elayer-content').css('padding', '0 ');
            }

            $('.elayer-mask').bind('click', function () {
                taget.closeall();
                $('.elayer-mask').unbind('click');
            });

            $('body').prepend(page);
            this.center(page);
            conf.init = false;
            return conf.id;
        }
    };
    return {
        loading: function () {
            return edoctor.loading();
        },
        //提示框   t:标题，c:内容，call关闭回调方法
        alert: function (t, c, okcall, closecall) {
            return edoctor.alert(t, c, okcall, closecall);
        },
        //询问框  t:标题，c:内容，call关闭回调, yescall回调方法，nocall回调方法
        confirm: function (t, c, yescall, nocall, closecall) {
            return edoctor.confirm(t, c, yescall, nocall, closecall);
        },
        //页面内容框(HTML)
        page: function (t, c, closecall, padding) {
            return edoctor.page(t, c, closecall, padding);
        },
        //无边框
        noborderpage: function (c, closecall, padding) {
            return edoctor.noborderpage(c, closecall, padding);
        },
        //PC预览手机页面
        mobilepage: function (c, closecall, padding) {
            return edoctor.mobilepage(c, closecall, padding);
        },
        //关闭
        close: function (i) {
            edoctor.close(i);
        },
        //关闭全部
        closeall: function () {
            edoctor.closeall();
        },
        //加载样式
        loadcss: function (url) {
            var path = edoctor.getjspath('elayer.js') == null ? edoctor.getjspath('elayer.min.js') : edoctor.getjspath('elayer.js');
            var style = document.createElement('style');
            style.setAttribute("type", "text/css");
            var styCss = "@import url('" + path + "elayer.css?q=1');";
            if (style.styleSheet) {
                style.styleSheet.cssText = styCss
            } else {
                style.appendChild(document.createTextNode(styCss))
            }
            document.getElementsByTagName("head")[0].appendChild(style);
        }
    };
})();
elayer.loadcss();